<?php

namespace App\Http\Controllers;

use App\Exceptions\HtkxException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function error( $data = '', $code= -1)
    {

        $result['code'] = $code;
        $result['message'] =(is_array($data) || empty($data))?'error':$data;
        $result['data'] = $data;
        $result['request'] = request()->all();

        return $result;
    }


    public function success($data = [],$url='')
    {
        return response()->json([
            'code'    => 0,
            'url'   =>$url,
            'status'    => true,
            'message' => (is_object($data) || is_array($data) || empty($data))?'success':$data,
            'data'    => $data,


        ]);
    }

    public function fail($code = -1, $data = [])
    {
        return response()->json([
            'code'    => $code,
            'status'    => false,
            //'message' => config('errorcode.code')[(int) $code],
            'data'    => $data,
        ]);
    }

    /**
     * @throws \App\Exceptions\HtkxException
     * 防止重复提交
     */
    public function repeatedSubmitCheck()
    {
        $key = md5(http_build_query(request()->all()));

        if(!empty(cache()->get($key)))
        {
            throw new HtkxException('重复提交',3001);
        }
        cache()->add($key ,1,3);
    }
}
